package com.cysion.study.ui.personal

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.other.color
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.image.RoundTransform
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import kotlinx.android.synthetic.main.activity_comment_detail.*

//点评详情
class CommentDetailActivity : BaseActivity() {
    override fun getLayoutId(): Int = R.layout.activity_comment_detail

    private var depositid=""
    private var courseId = ""
    private var stuid = ""
    override fun initView() {
        depositid = intent.extras.getString("depositid")
        courseId = intent.extras.getString("courseId")
        stuid = intent.extras.getString("stuid")
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("点评详情")
            setTexts("编辑", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.RIGHT) {
                    goComment()
                }else if (pos == TopBar.Pos.LEFT) {
                finish()
            }
            }

        }
        initFlowLayoutForPulishListener()
    }

    private fun initFlowLayoutForPulishListener() {
//        fl_pics.setShow(true)
        fl_pics.setonFlowLayoutForShowListener {
//            ShowToast("预览图片")
        }
    }


    private fun goComment() {
        var bundle=Bundle()
        bundle.putString("courseId", courseId)//课程id
        bundle.putString("stuid", stuid)//stuid学生id
        bundle.putString("depositid",depositid)
        bundle.putString("type","2")//1提交点评、2编辑
        UIHelper.jump(this,CommentActivity::class.java,bundle)
        finish()
    }

    //加载数据
    override fun initData() {
        super.initData()
        getDataList()
    }



    private fun getDataList() {
        Alert.loading(self)
        ApiClient.api.getCommentDetail(depositid,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    tv_content.text=it.content
                    /*if (TextUtils.isEmpty(it.imageurl)) {
                        iv_img.visibility = View.GONE
                    } else {
                        iv_img.visibility = View.VISIBLE
                        Glide.with(Box.context).load(it.imageurl)
                            .apply(RequestOptions.placeholderOf(R.mipmap.icon_app))
                            .apply(RequestOptions.bitmapTransform(RoundTransform(6)))
                            .into(iv_img)
                    }*/
                    var imgUrl=it.imageurl
                    if (!TextUtils.isEmpty(imgUrl)) {
                        fl_pics.visibility = View.VISIBLE
                        var strs = imgUrl.split(",") as MutableList
                       /* for ((index, obj) in strs.withIndex()) {
                            fl_pics.addLocationPic(index, false, obj)//显示图片
                        }*/
                        fl_pics.setShowData(true,strs,false,false)
                    } else {
                        fl_pics.visibility = View.GONE
                    }


                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }

    }



    override fun closeMvp() {
    }

}
